package com.hong.concurrent.chapter13;



import java.util.LinkedList;

/**
 * @Author csh
 * @Description
 * @Date 2018/7/8 11:36
 */
public class MessageQueue {
    private final LinkedList<Message> queue;
    /**最大值*/
    private final  static int DEFAULT_MAX_LIMIT = 100;
    /***/
    private final int limit;

    public MessageQueue() {
        this(DEFAULT_MAX_LIMIT);
    }

    public MessageQueue(final int limit) {
        this.limit = limit;
        this.queue = new LinkedList<>();
    }

    public void put(final  Message message) throws InterruptedException {
        synchronized (queue){
            while (queue.size()>limit){
                queue.wait();
            }

            queue.addLast(message);
            queue.notifyAll();
        }
    }

    public Message take() throws InterruptedException {
        synchronized (queue){
            while (queue.isEmpty()){
                queue.wait();
            }
            Message message = queue.removeFirst();
            queue.notifyAll();
            return message;
        }
    }

    public int getMaxLimit(){
        return  this.limit;
    }

    public int getMessageSize(){
        synchronized (queue){
            return  queue.size();
        }
    }

}
